<template>
  <div class="waihe">
    <div class="neihe">
      <div class="neihet">
        <h2>后台管理系统 <span>v4.0</span></h2>
      </div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="userName">
          <el-input v-model="ruleForm.userName" placeholder="登录账号" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input v-model="ruleForm.pwd" placeholder="登录密码" prefix-icon="el-icon-lock"  show-password></el-input>
        </el-form-item>
        <el-form-item prop="imgcode">
          <el-input v-model="ruleForm.imgcode" placeholder="验证码" prefix-icon="el-icon-picture"></el-input>
        </el-form-item>
        <img :src="src" alt="" class="bianing" @click="changCode">
        <el-form-item>
          <el-button type="primary" style="width: 100%;" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { MyLogin } from "@/api/login";
export default {
  data() {
    return {
      ruleForm: {
        userName: "蔡徐坤",
        pwd: 'aaa111',
        imgcode: '',
        k: 0,
        pdomain: 'zhangzhengyan'
      },
      src: "",
      rules: {
        userName: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        pwd: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        imgcode: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.getcode()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let { data: res } = await MyLogin(this.ruleForm);
          sessionStorage.setItem("token", res.token);

          this.$message({
            message: '登录成功',
            type: 'success',
            duration: "2000"
          });
          setTimeout(() => {
            this.$router.push('/HomeView')
          }, 2000);
        }
      });
    },
    getcode() {
      this.ruleForm.k = Math.random()
      this.src = "https://user.api.it120.cc/code?k=" + this.ruleForm.k
    },
    changCode() {
      this.getcode()
    }
  }
}
</script>
<style lang="scss" scoped>
.waihe {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
  height: 100vh;

  .neihe {
    width: 520px;
    height: 570px;
    color: white;
    color: white;
    margin-top: 160px;
    position: relative;

    ::v-deep .el-form-item__content {
      width: 450px;
      margin: 0 auto;
    }

    ::v-deep .el-input__inner {
      height: 52px;
      background-color: rgb(40, 52, 67);
      border: 1px solid rgb(69, 83, 100);
      border-radius: 5px;
      color: white;

    }

    .neihet {
      height: 60px;

      h2 {
        height: 30px;
        width: 100%;
        font-size: 25px;
        text-align: center;
        span {
        height: 17px;
        width: 31px;
        font-size: 14px;
        color: rgb(170, 170, 170);
      }
      }
    }
    .demo-ruleForm{
      position: relative;
    }

    .bianing {
      height: 40px;
      width: 80px;
      position: absolute;
      top: 154px;
      right: 45px;
    }
  }
}
</style>
